package org.jiuling.netty.test.handler.encoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.string.StringEncoder;
import org.jiuling.basics.converter.HexConvert;

import java.util.List;

/**
 * 字符串转字节解码器
 * @author 紫灵
 * @date 2025年07月04日 09时58分41秒
 */
public class StringToByteEncoder extends StringEncoder {

    @Override
    protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception {
        if (!msg.isEmpty()) {
            byte[] bytes = HexConvert.toBinary(msg.toString());
            ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes);
            out.add(byteBuf);
        }
    }


}
